package com.hmall.gateway.filters;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

//@Component
public class PrintAnyGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {
    //类的命令有讲究 以GatewayFilterFactory结尾，PrintAny为yml中指定的参数 - PrintAny=........ ，有参数就复杂了，以后用再学
    @Override
    public GatewayFilter apply(Object config) {
        //在这里构建你的过滤器

        return new OrderedGatewayFilter(new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                //写业务逻辑
//                ServerHttpRequest request = exchange.getRequest();
//                HttpHeaders headers = request.getHeaders();
//                System.out.println("headers = " + headers);
                System.out.println("自定义PrintAnyGatewayFilterFactory执行了！");
                chain.filter(exchange);
                return null;
            }
        },1);
    }
}
